package com.seu.me.test;

import com.alibaba.fastjson.JSONObject;
import com.seu.me.utils.Codec;
import com.seu.me.utils.MessageSign;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.TreeMap;
import java.util.UUID;

/**
 * Created by liangfeihu on 2018/10/30.
 */
public class MyTest7 {
    public static void main(String[] args) {
        RestTemplate restTemplate = new RestTemplate();

        String channelNo = "20010";
        //String aeskey = "3FADAE9950B216AF";
        String aeskey = "10DDH283D3CC6648";
        //String url = "http://jsqb-app-diandianhua-wyx.dev.cs.jisuqianbao.com/frontend/web/interface-ddh/reg";
        String url = "http://jsqb.kdqugou.com/frontend/web/interface-ddh/reg";
        // 业务请求参数处理
        String timestamp = System.currentTimeMillis() + "";

        String apply_no = UUID.randomUUID().toString().replace("-", "");

        // request 业务参数加密
        JSONObject applyInfo = new JSONObject();
        applyInfo.put("timestamp", timestamp);
        applyInfo.put("apply_terms", "");
        applyInfo.put("apply_loan", "");
        applyInfo.put("apply_amount", "");
        String encodedApplyInfo = Codec.strEncodBase64(aeskey, applyInfo.toJSONString());

        JSONObject userAttribute = new JSONObject();
        userAttribute.put("mobilephone", "15214353029");
        userAttribute.put("name", "");
        userAttribute.put("timestamp", timestamp);
        String encodedUserAttribute = Codec.strEncodBase64(aeskey, userAttribute.toJSONString());

        String pNameApplyNo = "apply_no";
        String pNameChannelNo = "channel_no";
        String pNameApplyInfo = "apply_info";
        String pNameUserAttr = "user_attribute";
        String pNameTimestamp = "timestamp";
        TreeMap<String, String> paramMap = new TreeMap<>();
        paramMap.put(pNameApplyNo, apply_no);
        paramMap.put(pNameChannelNo, channelNo);
        paramMap.put(pNameApplyInfo, encodedApplyInfo);
        paramMap.put(pNameUserAttr, encodedUserAttribute);
        paramMap.put(pNameTimestamp, timestamp);

        String sign = MessageSign.signParams(aeskey, paramMap);

        String params = pNameApplyNo + "=" + apply_no + "&" +
                pNameChannelNo + "=" + channelNo + "&" +
                pNameApplyInfo + "=" + encodedApplyInfo + "&" +
                pNameUserAttr + "=" + encodedUserAttribute + "&" +
                pNameTimestamp + "=" + timestamp + "&" +
                "sign=" + sign;
        //System.out.println("params=" + params);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
        paramsMap.add(pNameApplyNo, apply_no);
        paramsMap.add(pNameChannelNo, channelNo);
        paramsMap.add(pNameApplyInfo, encodedApplyInfo);
        paramsMap.add(pNameUserAttr, encodedUserAttribute);
        paramsMap.add(pNameTimestamp, timestamp);
        paramsMap.add("sign", sign);

        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(paramsMap, headers);
        ResponseEntity<JSONObject> exchange = restTemplate.exchange(url, HttpMethod.POST, requestEntity, JSONObject.class);
        System.out.println(exchange.getBody());

        // ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, params, String.class);
        // System.out.println(responseEntity.getBody());
        //String s = restTemplate.postForObject(url, params, String.class);
        //System.out.println(s);
    }
}
